//
//  PesonalCenterViewController.m
//  specialine
//
//  Created by 往返网络 on 17/3/16.
//  Copyright © 2017年 com. All rights reserved.
//

#import "PesonalCenterViewController.h"  //我的——个人中心（更改密码）
#import "ChangeViewController.h"  //更改姓名／所在地／公司名称的vc
#import "OldPSViewController.h"  //通过旧密码方式
#import "MessPSViewController1.h"  //通过手机验证码验证1
#import "AppDelegate.h"
#import "LoginViewController.h" //登陆界面
#import "PublicMethod.h"
#import "ZhifuPass1ViewController.h" //发送验证码
//调用相册／相机引用文件
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#define begin_x 30.0/W_BASE*SCREEN_W
#define min_h 80.0/H_BASE*SCREEN_H
#define userbtn_tag 1000
#define namebtn_tag 1001
#define local_tag 1002
#define company_tag 1003
#define loginps_ta 1004  //登录密码
#define cashps_tag 1005  //财务密码
#define payps_tag 1006 //支付密码
//定义修改登录获取验证码URL
#define URL_GETPASSWORDCODE [NSString stringWithFormat:@"%@/Appcustomernew/Personal/getCode?type=get",BASE_URL]
//定义修改支付密码获取验证码URL
#define URL_ZHIFUGECODE [NSString stringWithFormat:@"%@/Appcustomernewer/Pay/paySetPwSendCode?type=get",BASE_URL]

@interface PesonalCenterViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

{

    UIImageView * iconimage; //头像。。
    
    UILabel * userlab; //用户名
    
    UILabel * namelab; //姓名
    
    UILabel * localab; //地址
    
    UILabel * companylab ; //公司
    
    UILabel * loginlab; //登录
    
    UILabel * cashpslab; // 财务密码
    
    UILabel * paypslab; //支付密码
    
    UIImagePickerController * _imagePickerController;
    
    UIButton * exitbtn; //推出

}
@property (nonatomic,strong)AFHTTPSessionManager * manger;

@end

@implementation PesonalCenterViewController

- (void)viewWillAppear:(BOOL)animated{
    
    
    self.navigationController.navigationBar.hidden = YES;
    
    self.tabBarController.tabBar.hidden = YES;
    
    
}


- (void)viewWillDisappear:(BOOL)animated{
    
    
    self.navigationController.navigationBar.hidden = NO;
    
    self.tabBarController.tabBar.hidden = NO;
    
    
    
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self createUI];
    
    // Do any additional setup after loading the view.
}

- (void)createUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView * titleview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 128.0/1334*SCREEN_H)];
    
    titleview.backgroundColor =  COLORRGBA(7, 24, 57, 1);
    
    [self.view addSubview:titleview];
    
    
    float fontsize = 18;
    
    float corensize = 17;
    
    fontsize = SCREEN_H < 667 ? 16 : 20 ;
    
    corensize = SCREEN_H < 667 ? 15 : 19 ;
    
    
    if (SCREEN_H == 667) {
        
        fontsize = 18;
        
        corensize = 17;
        
    }
    
    UIButton * leftlable  = [[UIButton alloc]initWithFrame:CGRectMake(0, 20+14.0/1334*SCREEN_H, 80.0/W_BASE*SCREEN_W, 60.0/1334.0*SCREEN_H)];
    
    [leftlable setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    
    leftlable.titleLabel.font = [UIFont fontWithName:@"iconfont" size:fontsize+3];
    
    [leftlable setTitle:@"\U0000e697" forState:UIControlStateNormal];
    
    
    [leftlable.titleLabel setTextAlignment:NSTextAlignmentCenter];
    
    [leftlable addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [titleview addSubview:leftlable];
    
    UILabel * titlelab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(leftlable.frame), SCREEN_W, CGRectGetHeight(leftlable.frame))];
    
    titlelab.textAlignment = NSTextAlignmentCenter;
    
    titlelab.textColor = [UIColor whiteColor];
    
    titlelab.font = [UIFont systemFontOfSize:fontsize - 1];
    
    titlelab.text = @"个人中心";
    
    [titleview addSubview:titlelab];
    // 头像
    
    UILabel * iconlab = [[UILabel alloc]initWithFrame:CGRectMake(begin_x, CGRectGetMaxY(titleview.frame), 100.0/W_BASE*SCREEN_W, 130.0/H_BASE*SCREEN_H)];
    iconlab.text = @"头像";
    [self.view addSubview:iconlab];
    
    iconlab.font = [UIFont systemFontOfSize:fontsize - 4];
    
    UIView * breakline = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(iconlab.frame), SCREEN_W, 1)];
    
    breakline.backgroundColor = COLORRGBA(239, 239, 239, 1);
    
    [self.view addSubview:breakline];
    
    
    iconimage = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_W - 113.0/W_BASE*SCREEN_W, CGRectGetMinY(iconlab.frame)+23.0/H_BASE*SCREEN_H, 84.0/W_BASE*SCREEN_W, 84.0/W_BASE*SCREEN_W)];
    
    iconimage.image = [UIImage imageNamed:@"centericon_"];
    
    [self.view addSubview:iconimage];
    
    iconimage.layer.cornerRadius = fontsize + 2;
    
    
    iconimage.clipsToBounds = YES;
    
    
    UIButton * imagebtn = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(iconimage.frame), SCREEN_W, CGRectGetHeight(iconimage.frame))];
    [imagebtn addTarget:self action:@selector(tapNow) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:imagebtn];
    
    
    UILabel * yonghulab = [[UILabel alloc]initWithFrame:CGRectMake(begin_x, CGRectGetMaxY(breakline.frame), 100.0/W_BASE*SCREEN_W, min_h)];
    
    yonghulab.font = [UIFont systemFontOfSize:fontsize - 4];
    
    yonghulab.text = @"用户名";
    
    [self.view addSubview:yonghulab];
    
    userlab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(yonghulab.frame), CGRectGetMinY(yonghulab.frame), SCREEN_W - 205.0/W_BASE*SCREEN_W, min_h)];
    
    NSUserDefaults * userde = [NSUserDefaults standardUserDefaults];
    
    NSString * phone = [userde objectForKey:USERDEFAULTS_PHONE];
    
    userlab.textAlignment = NSTextAlignmentRight;
    
    userlab.text = phone;
    
    userlab.font = [UIFont systemFontOfSize:fontsize - 4];
    
    [self.view addSubview:userlab];
    
    UILabel * usericon = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(userlab.frame), CGRectGetMinY(userlab.frame), 45.0/W_BASE*SCREEN_W, min_h)];
    usericon.font = [UIFont fontWithName:@"iconfont" size:fontsize+5];
    usericon.text = @"\U0000e6a7";
    usericon.textColor = COLORRGBA(157, 157, 157, 1);
    [self.view addSubview:usericon];
//    usericon.backgroundColor = [UIColor yellowColor];
    usericon.textAlignment = NSTextAlignmentCenter;

    
    
    UIButton * userbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(userlab.frame), SCREEN_W, min_h)];
    userbtn.tag = userbtn_tag;
    
    userbtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [userbtn addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    
        [self.view addSubview:userbtn];

    NSArray * titlearr = @[@"登录密码",@"支付密码"];
    UIView  * view3 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(yonghulab.frame), SCREEN_W, min_h)];
    
    view3.backgroundColor = COLORRGBA(239, 239, 239, 1);
    
    [self.view addSubview:view3];
    
    UILabel * titlelabel2 = [[UILabel alloc]initWithFrame:CGRectMake(begin_x, view3.frame.origin.y, SCREEN_W - 100, min_h)];
    
    titlelabel2.text = @"安全设置";
    
    titlelabel2.textColor = COLORRGBA(102, 102, 102, 1);
    
    [self.view addSubview:titlelabel2];
    
    titlelabel2.font = [UIFont systemFontOfSize:fontsize - 5];

    UIView * view4 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(titlelabel2.frame), SCREEN_W, min_h*2)];
    
    view4.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:view4];

    for (int i = 0; i< 2; i++) {
        
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(begin_x, (i)*min_h, 200.0/W_BASE*SCREEN_W, min_h)];
        
        label.text = titlearr[i];
        
        [view4 addSubview:label];
        
        label.font = [UIFont systemFontOfSize:fontsize - 4];
        
       
        UILabel * usericon1 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(usericon.frame), CGRectGetMinY(label.frame), CGRectGetWidth(usericon.frame), min_h)];
        usericon1.font = [UIFont fontWithName:@"iconfont" size:fontsize+5];
        usericon1.text = @"\U0000e6a7";
        usericon1.textColor = COLORRGBA(157, 157, 157, 1);
        [view4 addSubview:usericon1];
        usericon1.textAlignment = NSTextAlignmentRight;

        UIButton * rightbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(label.frame), SCREEN_W, min_h)];
        
        rightbtn.tag = 1000+i+1+3;
        
        [rightbtn addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
        [view4 addSubview:rightbtn];
        
        UIView * breakline = [[UIView alloc]initWithFrame:CGRectMake(0, (i+1)*min_h, SCREEN_W, 1)];
        
        breakline.backgroundColor = COLORRGBA(239, 239, 239, 1);
        
        [view4 addSubview:breakline];
        
    }
    
   // 设置 rightlabel内容
    
    loginlab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(userlab.frame), CGRectGetMaxY(titlelabel2.frame), CGRectGetWidth(userlab.frame), min_h)];
    
    loginlab.font = [UIFont systemFontOfSize:fontsize - 4];
    
    loginlab.text = @"修改";
    
    loginlab.textAlignment = NSTextAlignmentRight;
    
    [self.view addSubview:loginlab];

    paypslab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(loginlab.frame), CGRectGetMaxY(loginlab.frame), CGRectGetWidth(loginlab.frame), min_h)];
    
    paypslab.font = [UIFont systemFontOfSize:fontsize - 4];
    paypslab.text = @"未设置";
    if ([self.is_password isEqualToString:@"1"]) {
        paypslab.text = @"";
    }
    paypslab.textColor = COLORRGBA(38, 146, 255, 1);
    
    paypslab.textAlignment = NSTextAlignmentRight;
    
    [self.view addSubview:paypslab];

    UIView * bottomview = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(paypslab.frame), SCREEN_W, SCREEN_H - CGRectGetMaxY(paypslab.frame))];
    
    bottomview.backgroundColor = COLORRGBA(239, 239, 239, 1);
    
    [self.view addSubview:bottomview];
    
     //创建uiimagepickercontroller对象
    
    _imagePickerController = [[UIImagePickerController alloc]init];
    
    _imagePickerController.delegate = self;
    
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
    _imagePickerController.allowsEditing = YES;
    
    exitbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREEN_H -260.0/H_BASE*SCREEN_H, SCREEN_W, min_h)];
    exitbtn.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:exitbtn];
    exitbtn.titleLabel.font = [UIFont boldSystemFontOfSize:fontsize-4];
    [exitbtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [exitbtn setTitleColor:COLORRGBA(255, 47, 47, 1) forState:UIControlStateNormal];
    [exitbtn addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    exitbtn.tag = 3000;
    
}

#pragma mark - -- - - - - -- 手势进入相册  － － － － －－
- (void)tapNow{

    ALERT(@"提示", @"暂未开通此服务", self, @"确定", nil);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction * photo = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //调用系统拍照
        [self selectImageFromCamera];
        }];
    
    UIAlertAction * xiance = [UIAlertAction actionWithTitle:@"从手机相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //进入相册
        [self selectImageFromAlbum];
        
    }];
    
    [alert addAction:cancel];
    
    [alert addAction:photo];
    
    [alert addAction:xiance];
    
//    [self presentViewController:alert animated:YES completion:nil];
    


}

- (void)enterClick:(UIButton *)btn{
    
    NSInteger tag = btn.tag;
    
    
    switch (tag) {
            
        case userbtn_tag:
            
            //用户名
            NSLog(@"用户名");
            ALERT(@"提示", @"暂未开通此服务", self, @"确定", nil);
            
            break;
            
        case namebtn_tag:
            
            //姓名
            
            NSLog(@"姓名");
            
            [self changeTitlewithString:1];
        
            break;
        case local_tag:
            
            //地址
            
            NSLog(@"地址");
            
            [self changeTitlewithString:2];
            
            break;
        case company_tag:
            
            //公司名称
            
            NSLog(@"公司名称");
            
            [self changeTitlewithString:3];

            break;
        case loginps_ta:
            
        { //登录密码
            NSLog(@"登录密码");
            [self changeLoginPs];
            break;
        }
        case cashps_tag:
            
        { //财务密码
            NSLog(@"修改密码");
            //请求发送验证码
            NSUserDefaults * userdef = [NSUserDefaults standardUserDefaults];
            NSString * phone = [userdef objectForKey:USERDEFAULTS_PHONE];
            [self getCode1:phone];
            
        }
            break;
        case payps_tag:
            
            //支付密码
            
            NSLog(@"支付密码");

            break;
        case 3000:
            //退出
            NSLog(@"退出");
            [self QUIT];
            
            break;
            
            
        default:
            
            [self.navigationController popViewControllerAnimated:YES];

            break;
            
    }
    
    
}

#pragma mark - - -- - - －－ － － －－ 推出，进入登录页面 － －－ － － －－ －－ － － －

- (void)QUIT{
    
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction * sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        // 放到公共方法里面，用于tocken失效时，处理
        AppDelegate * appdele = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        //清除本地信息
        NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
        [userdefault setObject:@"" forKey:USERDEFAULTS_PHONE];
        [userdefault setObject:@"" forKey:USERDEFAULTS_PASS];
        [userdefault setObject:@"" forKey:USERDEFAULTS_ISLOGIN];
        //  清除tocken
        [userdefault setObject:@"" forKey:USER_TOCKE];
        
        [self.navigationController popViewControllerAnimated:YES];
        //添加登录界面,在tabbarcontroller前面添加loginvc
        
        LoginViewController * loginvc = [[LoginViewController alloc]init];
        
        [ appdele.tabbarcontr.view addSubview:loginvc.view];
        
        [ appdele.tabbarcontr addChildViewController:loginvc];
        
        
    }];
    
    [alert addAction:cancel];
    
    [alert addAction:sure];
    
    [self presentViewController:alert animated:YES completion:nil];
    
    
}

#pragma mark --------------------- 从摄像头获取图片或视频
- (void)selectImageFromCamera
{
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    //录制视频时长，默认10s
    _imagePickerController.videoMaximumDuration = 15;
    
    //相机类型（拍照、录像...）字符串需要做相应的类型转换
//    _imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage];
    
    _imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];

    
    //视频上传质量
    //UIImagePickerControllerQualityTypeHigh高清
    //UIImagePickerControllerQualityTypeMedium中等质量
    //UIImagePickerControllerQualityTypeLow低质量
    //UIImagePickerControllerQualityType640x480
    _imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
    
    //设置摄像头模式（拍照，录制视频）为录像模式
   // _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
    
    //设置摄像头模式－－ － － －照片模式
    _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

    [self presentViewController:_imagePickerController animated:YES completion:nil];
    
    
    
    
}

#pragma mark 从相册获取图片或视频
- (void)selectImageFromAlbum
{
    
       NSLog(@"相册");
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:_imagePickerController animated:YES completion:nil];
    
    
}


#pragma mark ------------UIImagePickerControllerDelegate
//该代理方法仅适用于只选取图片时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    
    
    //在imagePickerController:didFinishPickingMediaWithInfo:代理方法中处理得到的资源，保存本地并上传...
    

    NSLog(@"选择完毕----image:%@-----info:%@",image,editingInfo);
    
    
}


//适用获取所有媒体资源，只需判断资源类型
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    NSLog(@"%@",info);
    
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    //判断资源类型
    
    NSLog(@"判断图片类型");
    
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
        
        
        UIImage * image = info[UIImagePickerControllerEditedImage];
        
        if (image != nil) {
            //图片显示在界面上
//            [changeImg setBackgroundImage:image forState:UIControlStateNormal];
            
            iconimage.image = info[UIImagePickerControllerEditedImage];

            //以下是保存文件到沙盒路径下
            //把图片转成NSData类型的数据来保存文件
            NSData *data;
            //判断图片是不是png格式的文件
            if (UIImagePNGRepresentation(image)) {
                //返回为png图像。
                
                NSLog(@"选择为 。。png");
                
                data = UIImagePNGRepresentation(image);
                
            }else {
                //返回为JPEG图像。
                
                NSLog(@"选择为jpg");
                
                data = UIImageJPEGRepresentation(image, 1.0);
            }
            //保存
//            [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];
            
        }
        
        //如果是图片
        iconimage.image = info[UIImagePickerControllerEditedImage];
        //压缩图片
        NSData *fileData = UIImageJPEGRepresentation(iconimage.image, 1.0);
        //保存图片至相册
        UIImageWriteToSavedPhotosAlbum(iconimage.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
        
        //上传图片
        
        [self uploadImageWithData:fileData];
        
        
    }else{
        
        //如果是视频
//        NSURL *url = info[UIImagePickerControllerMediaURL];
//        //播放视频
//        _moviePlayer.contentURL = url;
//        [_moviePlayer play];
//        //保存视频至相册（异步线程）
//        NSString *urlStr = [url path];
//        
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
//                
//                UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
//            }
//        });
//        NSData *videoData = [NSData dataWithContentsOfURL:url];
//        //视频上传
//        [self uploadVideoWithData:videoData];
        
        
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark--------- 图片保存完毕的回调--图片和视频保存完毕后的回调
- (void) image: (UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo: (void *)contextInf{
    
    
    
    
}

#pragma mark 视频保存完毕的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf{
    if (error) {
        NSLog(@"保存视频过程中发生错误，错误信息:%@",error.localizedDescription);
    }else{
        NSLog(@"视频保存成功.");
    }
}

#pragma mark - - - -- - - -- 上传图片 －－－－－－－－－－－

- (void)uploadImageWithData:(NSData *)data{

 //上传图片到服务器。...
    





}

#pragma mark  - - - - -- - 修改姓名／公司／所在地  － －－ － － － －
- (void)changeTitlewithString:(NSInteger )titletype{


    
    ChangeViewController * changevc = [[ChangeViewController alloc]init];

    if (titletype == 1) {
        
        changevc.titlestring = @"修改姓名";


    }
    else if (titletype == 2){
    
        changevc.titlestring = @"公司所在地";

    
    }
    else{
    
        changevc.titlestring = @"公司名称";

    
    }
    
    [self.navigationController pushViewController:changevc animated:YES];


    changevc.block = ^(NSString * changestring){
        
        //更改。。。
        
        if (titletype ==1) {
            
            namelab.text = changestring;
            
        }
        else if (titletype == 2){
        
        
            localab.text = changestring;
            
            
        }else{
        
            companylab.text = changestring;
            
        
        }
        
    };
    




}

#pragma mark  - - - -- - - - 修改登录密码- - - -- - - - -
- (void)changeLoginPs{


    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * cancel  = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction * oldps = [UIAlertAction actionWithTitle:@"通过旧密码方式" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
        //弹出旧密码页面
        
        OldPSViewController * oldvc = [[OldPSViewController alloc]init];
        
        [self.navigationController pushViewController:oldvc animated:YES];
        
        
    }];
    
    UIAlertAction * messps = [UIAlertAction actionWithTitle:@"通过手机验证码方式" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        //发送短信。。
        
        NSUserDefaults * userdef = [NSUserDefaults standardUserDefaults];
        
        NSString * phone = [userdef objectForKey:USERDEFAULTS_PHONE];
        
        [self getCode:phone];
        
    }];
    

    [alert addAction:cancel];
    
    [alert addAction:oldps];
    
    [alert addAction:messps];
    
    [self presentViewController:alert animated:YES completion:nil];
    


}

#pragma mark  - -- - - - -   获取验证码 - - - - - - - - - --
- (void)getCode:(NSString * )phonestr{
    
    
    //字符串反转之后，再发送出去 phonenum --- 反转 －－－＊2 －－－ 转化为a-z
    
    NSMutableString *s = [NSMutableString string];
    
    for (NSUInteger i = phonestr.length; i>0; i--) {
        
        [s appendString:[phonestr substringWithRange:NSMakeRange(i-1, 1)]];
        
    }
    
    NSInteger s1 = [s integerValue]*2;
    
    NSString * s2 = [NSString stringWithFormat:@"%ld",s1];
    
    
    NSArray * arr = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j"];
    
    NSMutableString * endstring = [NSMutableString string];
    
    for (NSUInteger i = 0; i < s2.length; i++) {
        
        //截取1个字符串
        NSString * s3 =  [s2 substringWithRange:NSMakeRange(i, 1)];
        
        NSInteger loc = [s3 integerValue];
        
        // 转化为小写字母
        
        NSString * s4 = arr[loc];
        
        [endstring appendString:s4];
        
        
    }
    
    //开始动画
    UIImageView * im = [PublicMethod startAnimation];
    
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    
    NSString * token = [user objectForKey:USER_TOCKE];
    
    NSString * url = [NSString stringWithFormat:@"%@&token=%@&mobile=%@",URL_GETPASSWORDCODE,token,endstring];
    
    
    [self.manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (responseObject) {
            
            NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            NSInteger code = [dict[@"code"]integerValue];
            
            if (code == 0) {
                
                //获取验证码成功
                
                [im stopAnimating];
                
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
                
                //通过短信方式
                
                MessPSViewController1  * messvc = [[MessPSViewController1 alloc]init];
                
                [self.navigationController pushViewController:messvc animated:YES];
                

            }else{
                
                [im stopAnimating];
                
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
            }
            
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [im stopAnimating];
        
        ALERT(@"提示", @"网络请求失败", self, @"确定", nil);
        
    }];
    
    
    
}
#pragma mark - - - - --------获取支付验证码 － － － － －－ － － － －－ － －
- (void)getCode1:(NSString * )phonestr{
        
    
    //开始动画
    UIImageView * im = [PublicMethod startAnimation];
    
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    
    NSString * token = [user objectForKey:USER_TOCKE];
    
    NSString * url = [NSString stringWithFormat:@"%@&token=%@",URL_ZHIFUGECODE,token];
    NSLog(@"获取支付密码url %@",url);
    [self.manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (responseObject) {
            
            NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            NSInteger code = [dict[@"code"]integerValue];
            
            if (code == 0) {
                
                //获取验证码成功
                
                [im stopAnimating];
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                //进入修改支付密码界面
                
                ZhifuPass1ViewController * zhifupass = [[ZhifuPass1ViewController alloc]init];
                [self.navigationController pushViewController:zhifupass animated:YES];
            }else{
                
                [im stopAnimating];
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
            }
            
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [im stopAnimating];
        
        ALERT(@"提示", @"网络请求失败", self, @"确定", nil);
        
    }];
    
    
}


- (AFHTTPSessionManager*)manger{
    
    _manger = [AFHTTPSessionManager manager];
    
    _manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    _manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    
    return _manger;
    
}

- (void)didReceiveMemoryWarning {
    
    
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
